/*
 * SOLMIX PROJECT
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

package org.solmix.web.box.client;

import org.solmix.web.box.client.ui.AppMenuBar;
import org.solmix.web.box.client.ui.DeskSwitchBar;
import org.solmix.web.box.client.ui.TaskBar;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Administrator
 * @version $Id$ 2012-2-12
 */

public class BoxWrapper extends Composite
{

    final AbsolutePanel box;

    private DecoratedPopupPanel popupPanel;

    AppMenuBar appMenuBar;

    final DockPanel mainPanel;

    public BoxWrapper()
    {
        box = new AbsolutePanel();
        adaptSize(box);
        initWidget(box);
        appMenuBar = new AppMenuBar();
        popupPanel = createPopupMenu();
        // Because of css style can not used to full widget's background,so use image to do it.
        final Image backgroundImage = new Image("/images/background/1.jpg");
        // final Image backgroundImage = new Image("/images/slx_loading.gif");
        adaptSize(backgroundImage);

        mainPanel = new DockPanel();
        adaptSize(mainPanel);
        mainPanel.setStyleName("boxWrapper");

        box.add(backgroundImage, 0, 0);
        box.add(mainPanel, 0, 0);
        // add resize handler to anto resize window size.
        Window.addResizeHandler(new ResizeHandler() {

            @Override
            public void onResize(ResizeEvent event) {
                adaptSize(box);
                adaptSize(backgroundImage);
                adaptSize(mainPanel);
            }

        });

        // title container bar
        TaskBar titleBar = new TaskBar();

        mainPanel.add(titleBar, DockPanel.SOUTH);// .addSouth(titleBar, 64.0);
        DeskSwitchBar deskSwitchBar = new DeskSwitchBar();
        mainPanel.add(deskSwitchBar, DockPanel.NORTH);// .addNorth(deskSwitchBar, 48.0);

        mainPanel.add(appMenuBar, DockPanel.WEST);// .addWest(appMenuBar, 72.0);
        // appMenuBar.addClickHandler(new RightClickHandler() {
        //
        // @Override
        // public void onRightClick(Widget sender, Event event) {
        // int x = DOM.eventGetClientX(event);
        // int y = DOM.eventGetClientY(event);
        // popupPanel.setPopupPosition(x, y);
        // popupPanel.show();
        // }

        // });
        // center desktop
        SimplePanel desktop = new SimplePanel();

        mainPanel.add(desktop, DockPanel.CENTER);
    }

    private void adaptSize(Widget widget) {
        int _w = Window.getClientWidth();
        int _h = Window.getClientHeight();
        widget.setSize(_w + "px", _h + "px");
    }

    Command showAlertCommand = new Command() {

        public void execute() {
            Window.alert("Hope this example helps.");
        }
    };

    Command showImageCommand = new Command() {

        public void execute() {
        }
    };

    Command showSponserCommand = new Command() {

        public void execute() {
            mainPanel.removeFromParent();
            popupPanel.hide();
            mainPanel.add(appMenuBar, DockPanel.EAST);
        }
    };

    public DecoratedPopupPanel createPopupMenu() {
        MenuBar popupMenuBar = new MenuBar(true);

        MenuItem alertItem = new MenuItem("<img src='/images/icons/16/disk_blue.png'/><b>show came</b>", true, showAlertCommand);
        MenuItem imageItem = new MenuItem("Show Oliver ", true, showImageCommand);
        MenuItem sponserItem = new MenuItem("靠右边 ", true, showSponserCommand);
        DecoratedPopupPanel popupPanel = new DecoratedPopupPanel(true);
        // popupPanel.setStyleName("popup");

        // alertItem.addStyleName("popup-item");
        // imageItem.addStyleName("popup-item");
        // sponserItem.addStyleName("popup-item");
        popupMenuBar.addItem(alertItem);
        popupMenuBar.addItem(imageItem);
        popupMenuBar.addItem(sponserItem);

        popupMenuBar.setVisible(true);
        popupPanel.setWidget(popupMenuBar);
        // popupPanel.setWidget(new HTML("dsdfsdf"));
        return popupPanel;
    }

}
